/* 
 * Copyright (c) 2012, Fromentin Xavier, Schnell Michaël, Dervin Cyrielle, Brabant Quentin
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *      * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *      * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *      * The names of its contributors may not be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Fromentin Xavier, Schnell Michaël, Dervin Cyrielle OR Brabant Quentin 
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package kameleon.document;

import java.util.Iterator;

import kameleon.exception.InvalidIndexException;
import kameleon.exception.KameleonException;

/**
 * Represents a text paragraph contained in a document.
 * 
 * <p>Text paragraphs can contain different elements : simple text, links, 
 * images, line breaks, ...
 * 
 * <p>Standard properties include : alignment.
 * 
 * @author		Xavier Fromentin, Schnell Michaël, Dervin Cyrielle, Brabant Quentin
 * @version		1.0
 * 
 * @see			TextParagraphElement
 */
public class TextParagraph extends Paragraph 
implements Iterable<TextParagraphElement> {

	/**
	 * Needed to serialize this class.
	 * 
	 * @see		java.io.Serializable
	 */
	private static final long serialVersionUID = -3660417831796839617L ;

	/**
	 * Default alignment used when creating an instance.
	 */
	private static final Integer DEFAULT_TEXT_ALIGNMENT = LEFT_ALIGNMENT ;

	/**
	 * {@code Element}s contained in this text paragraph.
	 */
	protected Elements elements ;

	/**
	 * Builds an empty instance with default properties.
	 * 
	 * <p>The default properties are the following :
	 * <ul>
	 * <li>alignment: left
	 * </ul>
	 */
	public TextParagraph() {
		super() ;
		this.elements = new Elements() ;
		try {
			this.setProperty(TEXT_ALIGNMENT, DEFAULT_TEXT_ALIGNMENT) ;
		} catch(KameleonException ka) {/* This should not happen. */}
	}// TextParagraph()

	/**
	 * Add an instance of {@code TextParagraphElement} at the specified index.
	 * 
	 * @param 	p
	 * 			instance of {@code TextParagraphElement} added
	 * 
	 * @param 	index
	 * 			insert position
	 * 
	 * @throws	InvalidIndexException
	 * 			if the given index is out of range
	 * 			{@code index < 0 || index >= getCount()}
	 * 
	 * @see		Elements#add(TextParagraphElement, int)
	 */
	public void add(TextParagraphElement p, int index) throws InvalidIndexException {
		this.elements.add(p, index) ;
	}// add(TextParagraphElement, int)

	/**
	 * Appends an instance of {@code TextParagraphElement} at the end of this instance
	 * of {@code TextParagraph}.
	 * 
	 * @param 	p
	 * 			instance of {@code TextParagraphElement} appended
	 * 
	 * @see		Elements#append(TextParagraphElement)
	 */
	public void append(TextParagraphElement p) {
		this.elements.append(p) ;
	}// append(TextParagraphElement)

	/**
	 * Removes the instance of {@code Paragraph} found at the given index.
	 * 
	 * @param 	index
	 * 			index of the removed {@code Paragraph}
	 * 
	 * @throws	InvalidIndexException
	 * 			if the given index is out of range
	 * 			{@code index < 0 || index >= getCount()}
	 * 
	 * @see		Elements#remove(int)
	 */
	public void remove(int index) throws InvalidIndexException {
		this.elements.remove(index) ;
	}// remove(int)

	/**
	 * Prepends an instance of {@code TextParagraphElement}.
	 * 
	 * @param 	pe
	 * 			instance of {@code TextParagraphElement} prepended
	 */
	public void push(TextParagraphElement pe) {
		this.elements.push(pe) ;
	}// push(TextParagraphElement)

	/**
	 * Removes all the instances matching the given {@code TextParagraphElement}.
	 * 
	 * @param 	p
	 * 			removed {@code TextParagraphElement}
	 * 
	 * @see		Elements#remove(TextParagraphElement)
	 */
	public void remove(TextParagraphElement p) {
		this.remove(p) ;
	}// remove(TextParagraphElement)

	/**
	 * Returns the number of instances of {@code TextParagraphElement} contained 
	 * in this instance.
	 * 
	 * @return	Number of instances of {@code TextParagraphElement} contained 
	 * 			in this instance
	 */
	public int getCount() {
		return this.elements.getCount() ;
	}// getCount()

	/**
	 * Returns an iterator for this instance.
	 * 
	 * <p>The iterator iterates over the text paragraph elements sequentially.
	 * 
	 * @return	an instance of {@code Iterator<TextParagraphElement>} for this instance
	 */
	@Override
	public Iterator<TextParagraphElement> iterator() {
		return this.elements.iterator() ;
	}// iterator()

}// class TextParagraph